package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext ctx;
    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    @Transactional(readOnly = true)
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);

    }

    @Override
    @Transactional(readOnly = true)
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
     permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo(permissions);
    }

    @Override
    public void reload() {
        //获取数据库中的所有权限表达式
        List<Permission> permissions = permissionMapper.selectAll();
        //1.获取所有的controller
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = map.values();
       //遍历每个controller，获取里面的每个方法
        for (Object controller:controllers){
            //如果不是cglib代理对象,不往下执行
            if(!AopUtils.isAopProxy(controller)) {
               continue;
            }
            //controller上如果贴了shiro注解.shiro就会对该controller进行cglib继承代理
            //这时候从容器中获取到的就是代理对象,由于代理类无法获取父类上的注解,所以需要先拿父类,再从父类中获取注解
            Class clazz = controller.getClass().getSuperclass();
            Method[] methods = clazz.getDeclaredMethods();
            //3.遍历每个方法，获取方法上的RequiredPermission，判断该方法上是否有贴上自定义注解
            for (Method method:methods) {
                //获取方法上的RequiredPermission
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if(annotation!=null){
                    //4.获取注解上的权限名称，以及权限表达式
                    //直接从注解上获取
                    String expression = annotation.value()[0];
                    if(!permissions.contains(expression)){
                    //5.封装成权限对象
                    String name = annotation.value()[1];
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    //6.把对象插入到数据库中
                    permissionMapper.insert(permission);
                    }
                }
            }
        };
    }

    @Override
    public List<String> selectByElId(Long employeeId) {
        return permissionMapper.selectByElId(employeeId);
    }
}
